package com.example.ephuizi.stl.util.string_3d;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ephuizi@gmail.com on 2015/11/5.
 */
public class Data3D {
    List<Triangle> tris;
    Point3D max = new Point3D();
    Point3D min = new Point3D();

    public Data3D(){
        this.tris = new ArrayList<>();
    }
    public Data3D(List<Triangle> tris) {
        this.tris = tris;
    }

    public void setMax(float x, float y, float z) {
        max.x = x;
        max.y = y;
        max.z = z;
    }

    public void setMin(float x, float y, float z) {
        min.x = x;
        min.y = y;
        min.z = z;
    }

    public void updateMax(float x, float y, float z) {
        if (max.x < x)
            max.x = x;
        if (max.y < y)
            max.y = y;
        if (max.z < z)
            max.z = z;

    }

    public void updateMax(Triangle tri) {
        updateMax(tri.x1, tri.y1, tri.z1);
        updateMax(tri.x2, tri.y2, tri.z2);
        updateMax(tri.x3, tri.y3, tri.z3);
    }

    public void updateMin(float x, float y, float z) {
        if (min.x > x)
            min.x = x;
        if (min.y > y)
            min.y = y;
        if (min.z > z)
            min.z = z;
    }

    public void updateMin(Triangle tri) {
        updateMin(tri.x1, tri.y1, tri.z1);
        updateMin(tri.x2, tri.y2, tri.z2);
        updateMin(tri.x3, tri.y3, tri.z3);
    }

    public Point3D getCenter() {
        Point3D center = new Point3D();
        center.x = max.x + min.x;
        center.y = max.y + min.y;
        center.z = max.z + min.z;
        return center;
    }

    public void calculateMaxAndMin() {
        if (tris.size() > 0) {
            Triangle first = tris.get(0);
            setMax(first.x1, first.y1, first.z1);
            setMin(first.x1, first.y1, first.z1);

            for (int i = 0; i < tris.size(); i++) {
                Triangle tri = tris.get(i);
                updateMax(tri);
                updateMin(tri);
            }
        }
    }
}
